deferred class IUP_WIDGET_TEXT_COMMON
-- Common attributes related with text controls.

inherit
	IUP_WIDGET_INTERNALS
	IUP_WIDGET_PADDING
	
insert
	IUP_INTERFACE

feature {ANY}

	clipboard (option: STRING)
		-- (write-only): clear, cut, copy or paste the selection to or from the 
		-- clipboard. Values: "CLEAR", "CUT", "COPY" or "PASTE". In Windows UNDO 
		-- is also available, and REDO is available when FORMATTING=True.
		require
			is_valid_option(option)
		do
			iup_open.set_attribute(Current, "CLIPBOARD", option)
		end

	set_cue_banner (txt: STRING)
		-- [Windows and GTK Only] (non inheritable): a text that is displayed 
		-- when there is no text at the control. It works as a textual cue, or 
		-- tip to prompt the user for input. Valid only for MULTILINE=False, and 
		-- works only when Visual Styles are enabled. (since 3.0) [GTK 3.2] (GTK 
		-- support added in IUP 3.20).
		do
			iup_open.set_attribute(Current, "CUEBANNER", txt)
		end

	set_filter (filter: STRING)
		-- [Windows Only] (non inheritable): allows a custom filter to process 
		-- the characters: Can be LOWERCASE, UPPERCASE or NUMBER (only numbers 
		-- allowed).
		require
			is_valid_filter(filter)
		do
			iup_open.set_attribute(Current, "FILTER", filter)
		end

	set_mask (mask: STRING)
		-- (non inheritable): Defines a mask that will filter interactive text 
		-- input. Use "REMOVE" to remove the mask.
		--
		-- Since the validation process is performed key by key when the user is 
		-- typing, an intermediate value cannot be typed if it does not follow 
		-- the mask rules.
		--
		-- If you set the VALUE attribute any text can be used. To set a value 
		-- that is validated by the current MASK use VALUEMASKED.
		--
		-- Pre-Defined Masks
		--
		-- Definition 	    Value         Description
		-- IUP_MASK_INT 	"[+/-]?/d+"    integer number
		-- IUP_MASK_UINT 	"/d+"          unsigned integer number
		-- IUP_MASK_FLOAT "[+/-]?(/d+/.?/d*|/./d+)" floating point number
		-- IUP_MASK_UFLOAT "(/d+/.?/d*|/./d+)" unsigned floating point number
		-- IUP_MASK_EFLOAT "[+/-]?(/d+/.?/d*|/./d+)([eE][+/-]?/d+)?" floating 
		--                               point number with exponential notation
		-- IUP_MASK_FLOATCOMMA 	"[+/-]?(/d+/,?/d*|/,/d+)" floating point number
		-- IUP_MASK_UFLOATCOMMA "(/d+/,?/d*|/,/d+)" unsigned floating point number
		--
		-- For more information see:
		-- http://webserver2.tecgraf.puc-rio.br/iup/en/attrib/iup_mask.html
		do
			if mask.is_equal("REMOVE") then
				iup_open.set_attribute_null(Current, "MASK")
			elseif mask_dictionary.has(mask) then
				iup_open.set_attribute(Current, "MASK", mask_dictionary.at(mask))
			else
				iup_open.set_attribute(Current, "MASK", mask)
			end
		end

	set_mask_case_insesitive (state: BOOLEAN)
		-- If True, will turn the filter case insensitive. Default: False.
		do
			iup_open.set_attribute(Current, "MASKCASEI", boolean_to_yesno(state))
		end

	set_mask_no_empty (state: BOOLEAN)
		-- If True, value can NOT be Void or empty. Default: False (can be 
		-- empty or Void).
		do
			iup_open.set_attribute(Current, "MASKNOEMPTY", boolean_to_yesno(state))
		end

	set_mask_decimal_symbol (value: STRING)
		-- The decimal symbol for string/float conversion. Can be "." or ",". 
		-- Must be set before MASKFLOAT.
		require
			is_valid_symbol (value)
		do
			iup_open.set_attribute(Current, "MASKDECIMALSYMBOL", value)
		end

	set_mask_integer (min, max: INTEGER)
		-- Defines an integer mask with limits "min-max". It will replace MASK 
		-- using one of the pre-defined masks.
		local
			str: STRING
		do
			str := min.to_string
			str.append_string(":")
			str.append_string(max.to_string)

			iup_open.set_attribute(Current, "MASKINT", str)
		end

	set_mask_float (min, max: FLOAT)
		-- Defines a floating point mask with limits "min:max". It will replace 
		-- MASK using one of the pre-defined masks.
		local
			str: STRING
		do
			str := min.to_string
			str.append_string(":")
			str.append_string(max.to_string)

			iup_open.set_attribute(Current, "MASKFLOAT", str)
		end

	set_number_of_characters (num: INTEGER)
		-- Maximum number of characters allowed for keyboard input, larger text 
		-- can still be set using attributes. The maximum value is the limit of 
		-- the VALUE attribute. The "0" value don't impose a limit. Default: "0".
		require
			num >= 0
		do
			iup_open.set_attribute(Current, "NC", num.to_string)
		end

	set_readonly (state: BOOLEAN)
		-- Allows the user only to read the contents, without changing it. 
		-- Restricts keyboard input only, text value can still be changed using 
		-- attributes. Navigation keys are still available. Possible values: 
		-- "True", "False". Default: False.
		do
			iup_open.set_attribute(Current, "READONLY", boolean_to_yesno(state))
		end

	set_selected_text (value: STRING)
		-- Replaces the current selection. Similar to INSERT, but does nothing if 
		-- there is no selection.
		do
			iup_open.set_attribute(Current, "SELECTEDTEXT", value)
		end

	get_selected_text: STRING
		-- Selected text. Returns "" if there is no selection.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "SELECTEDTEXT")

			if str /= Void then
				Result := str
			else
				Result := ""
			end
		end

	select_all
		-- Select all the text.
		do
			iup_open.set_attribute(Current, "SELECTION", "ALL")
		end

	deselect_all
		-- Deselect.
		do
			iup_open.set_attribute(Current, "SELECTION", "NONE")
		end

	-- Changes

	append (txt: STRING)
		-- Inserts a text at the end of the current text.
		do
			iup_open.set_attribute(Current, "APPEND", txt)
		end

   insert (txt: STRING)
		-- (write-only): Inserts a text in the caret's position, also replaces 
		-- the current selection if any. Ignored if set before map.
		do
			iup_open.set_attribute(Current, "INSERT", txt)
		end

feature {IUP_TEXT, IUP_MULTILINE}

	create_mask_dictionary
		do
			mask_dictionary := {HASHED_DICTIONARY[STRING, STRING] <<
					   "[+/-]?(/d+/.?/d*|/./d+)", "IUP_MASK_FLOAT";
						"(/d+/.?/d*|/./d+)", "IUP_MASK_UFLOAT";
						"[+/-]?(/d+/.?/d*|/./d+)([eE][+/-]?/d+)?", "IUP_MASK_EFLOAT";
					   "[+/-]?(/d+/,?/d*|/,/d+)", "IUP_MASK_FLOATCOMMA";
						"(/d+/,?/d*|/,/d+)", "IUP_MASK_UFLOATCOMMA";
						"[+/-]?/d+", "IUP_MASK_INT";
						"/d+", "IUP_MASK_UINT" >> }
		end

feature {}

	-- Mask dictionary
	
	mask_dictionary: DICTIONARY[STRING, STRING]

	-- Internals

	is_valid_option (value: STRING): BOOLEAN
		do
			if value.is_equal("CLEAR") or
				value.is_equal("CUT") or
				value.is_equal("COPY") or
				value.is_equal("PASTE") or
				value.is_equal("UNDO") or
				value.is_equal("REDO") then
				Result := True
			else
				Result := False
			end
		end

	is_valid_filter (value: STRING): BOOLEAN
		do
			if value.is_equal("LOWERCASE") or
				value.is_equal("UPPERCASE") or
				value.is_equal("NUMBER") then
				Result := True
			else
				Result := False
			end
		end

	is_valid_symbol (value: STRING): BOOLEAN
		do
			if value.is_equal(".") or
				value.is_equal(",") then
				Result := True
			else
				Result := False
			end
		end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2018, 2019 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
